﻿using System.Linq;
using Churchtoolkit.Utils.EmailProvider;
using Churchtoolkit.Web.Shell.Views;
using Churchtoolkit.Utils.MVP;
using Churchtoolkit.DataAccess.Repositories;
using Churchtoolkit.DataAccess.Entities;
using Churchtoolkit.DataAccess.Common;
using System.Net.Mail;
using Churchtoolkit.Utils.Authentication;
using System.Web.Security;


namespace Churchtoolkit.Web.Shell.Presenters
{
    public class LoginPresenter : Presenter<ILoginView>
    {
        #region [ Fields ]
        private readonly MembersRepository _memberRepository; 
        private readonly IEmailProvider _emailProvider;
        private Members _member;
        private bool _isActivatedFromConfig;
        #endregion

        #region [ Constructor ]
        public LoginPresenter(MembersRepository memberRepository,IEmailProvider emailProvider)
        {
            _memberRepository = memberRepository;
            _emailProvider = emailProvider;
        }
        #endregion

        #region [ Methods ]
        /// <summary>
        /// Members the exists.
        /// </summary>
        /// <param name="email">The email.</param>
        /// <param name="password">The password.</param>
        /// <returns></returns>
        public bool MemberExists(string email, string password)
        {
            EntityResult<Members> accounts = _memberRepository.Find(items =>
                                                                        {
                                                                            items.Email = email;
                                                                            items.Password = password;
                                                                        });

            if (accounts.RowsCount == 0)
            {
                // check if this user is registered in config if yes save the value for later on else 
                // return false.
                _isActivatedFromConfig = FormsAuthentication.Authenticate(email, password);
                return _isActivatedFromConfig;
            }
            _member = accounts.Single();
            return true;
        }

        /// <summary>
        /// Members the is not active.
        /// </summary>
        /// <returns></returns>
        public bool MemberIsNotActive()
        {
            if (_member == null)
                return _isActivatedFromConfig;
            return _member.IsActive;
        }

        /// <summary>
        /// Called when [password forget].
        /// </summary>
        /// <param name="email">The email.</param>
        /// <returns></returns>
        public bool OnPasswordForget(string email)
        {
            EntityResult<Members> accounts = _memberRepository.Find(item => { item.Email = email; });

            if (accounts.RowsCount == 0)
                return false;

            var user = accounts.Single();
            string password = user.Password.Decrypt(Constants.ApplicationKey);
            _emailProvider.SendMail(user.Email, "0002", user.DisplayName, password);
            user.IsActive = false;
            _memberRepository.Update(user);
            return true;
        }

        #endregion
    }
}

